<template>
	<view class="register">
	
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<view class="main-list oBorder">
					<input 
						class="main-input" 
						v-model="phoneData" 
						type="text" 
						maxlength="11" 
						placeholder="手机号" 
					/>
				</view>
				<view class="main-list oBorder">
					<input 
						class="main-input" 
						v-model="passData" 
						type="text" 
						maxlength="32" 
						placeholder="登录密码" 
						:password="!showPassword" 
					/>
					<image 
						:class="['img',showPassword?'cuIcon-attention':'cuIcon-attentionforbid']" 
						@tap="isShowPass"
					></image>
				</view>
				<view class="main-list oBorder"> 
					<input 
						class="main-input" 
						v-model="verCode" 
						type="number" 
						maxlength="4" 
						placeholder="验证码" 
					/>
					<view 
						:class="['vercode',{'vercode-run': second>0}]" 
						@tap="getVerCode()"
					>{{ getVerCodeSecond }}</view>
				</view>
				
			</view>
			
			<button 
				:class="['buttonBorder',!isRotate?'dlbutton':'dlbutton_loading']" 
				@tap="startReg()"
			>
				<view :class="isRotate?'rotate_loop':''">
					<text v-if="isRotate" class="cuIcon-loading1 "></text>
					<text v-if="!isRotate">注 册</text>
				</view>
			</button>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				>同意</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this,countDown;
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', // 用户/电话
				passData:'', //密码
				showPassword:false, //密码是否显示
				second: 0, //倒计时
				verCode:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		mounted() {
			_this= this;
			clearInterval(countDown);//先清理一次循环，避免缓存
		},
		methods: {
			isShowPass() {
				//是否显示密码
			    _this.showPassword = !_this.showPassword
			},
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode(){
				//获取验证码
				if (_this.phoneData.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				if(_this.second>0){
					return false;
				}
				_this.second = 60;
				countDown = setInterval(function(){
					_this.second--;
					if(_this.second==0){
						clearInterval(countDown)
					}
				},1000)
				console.log("获取验证码")
			},
		    startReg() {
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《软件用户协议》'
				    });
				    return false;
				}
				if (this.phoneData.length !=11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
		        if (this.passData.length < 6) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return false;
		        }
				if (this.verCode.length != 4) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}
				console.log("注册成功")
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
		    }
		},
		computed:{
			getVerCodeSecond(){
				//验证码倒计时计算
				if(this.second<=0){
					return '获取验证码';
				}else{
					if(this.second<10){
						return '0'+this.second;
					}else{
						return this.second;
					}
				}
				
			}
		},
	}
</script>

<style>
	@import url("../../login/css/main.css");
</style>